package com.lj.demo.orderService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.demo.commonutils.JwtUtils;
import com.lj.demo.commonutils.R;
import com.lj.demo.orderService.entity.TOrder;
import com.lj.demo.orderService.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author liujiang
 * @since 2021-01-08
 */
@Api(description = "订单管理")
@RestController
@RequestMapping("/orderService/order")
@CrossOrigin
public class TOrderController {


    @Autowired
    private TOrderService orderService;

    @ApiOperation("根据课程id存入订单")
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String orderId = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("item",orderId);
    }

    @ApiOperation("根据订单id获取订单信息")
    @GetMapping("getOrder/{orderId}")
    public R getOrder(@PathVariable String orderId){
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);
        TOrder order = orderService.getOne(queryWrapper);
        return R.ok().data("item",order);
    }

    @ApiOperation("根据用户id和课程id查询订单信息")
    @GetMapping("isBuyCourse/{memberId}/{id}")
    public Boolean isBuyCourse(@PathVariable String memberId,
                               @PathVariable String id){
        //订单状态1表示支付成功
        int count = orderService.count(new QueryWrapper<TOrder>().eq("member_id",memberId).eq("course_id",id).eq("status",1));

        if (count>0){
            return true;
        }return false;
    }











}

